<?php
class Gallery_model extends CI_Model {

        public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();
        }

        public function get_approved_images($limit,$start)
        {
            //$this->db->limit($limit,$start);
            $query = $this->db->where('status','1')->limit($limit,$start)->order_by("id", "desc")->get("photos_lists");
            
    		if ($query->num_rows()>0){
    			return $query->result_array();
    		}
    		else{
    			return FALSE;
    		}
        }
    
        public function approved_images_count()
        {
            $query = $this->db->where('status','1')->get("photos_lists");
            return $query->num_rows();
        }
        
        public function insert_vote_img($fb_id,$fb_name,$fb_email,$img_id)
        {   
            //check the existence of the voter
            $query = $this->db->where('fb_id',$fb_id)->get("voters_lists");
            
            if ($query->num_rows()>0){
                //combine a new img id with the existing one
                $this->db->select('vote_img')->from('voters_lists')->where('fb_id',$fb_id);
                $voted_img_id = $this->db->get();
                foreach($voted_img_id->result() as $row){
                    $old_voted_img_id = $row->vote_img;
                }
                
                $extracted_img_id = explode(',',$old_voted_img_id);
                array_push($extracted_img_id,$img_id);
                
                if (strpos($old_voted_img_id,',') !== false){
                    //more than one image id
                    $extracted_img_id = explode(',',$old_voted_img_id);
                    array_push($extracted_img_id,$img_id);
                    $combined_img_id = implode(',',$extracted_img_id);
                    $upadte_img_id = array('vote_img'=> $combined_img_id);
                    $this->db->where('fb_id',$fb_id )->update('voters_lists',$upadte_img_id);
                } else{
                    //only one image id
                    $extracted_img_id = array($old_voted_img_id,$img_id);
                    $combined_img_id = implode(',',$extracted_img_id);
                    $upadte_img_id = array('vote_img'=> $combined_img_id);
                    $this->db->where('fb_id',$fb_id )->update('voters_lists',$upadte_img_id);
                }

            } else {
                //insert a new fb user
                $insert_list = array('fb_id' => $fb_id,
                                     'fb_name' => $fb_name,
                                     'fb_email' => $fb_email,
                                     'vote_img' => $img_id
                );
                $this->db->insert('voters_lists', $insert_list);  
            }
        }
        
        public function update_vote_results($img_id){
            $this->db->select('results')->from('photos_lists')->where('id',$img_id);
            $query = $this->db->get();
            foreach($query->result() as $row){
                    $new_voted_img_id = $row->results + 1;
            }
            $upadte_results = array('results'=> $new_voted_img_id);
            $this->db->where('id',$img_id )->update('photos_lists',$upadte_results);
        }
        
        public function get_all_voter_img($fb_id){
            $this->db->select('vote_img')->from('voters_lists')->where('fb_id',$fb_id);
            $voted_img_id = $this->db->get();
                foreach($voted_img_id->result() as $row){
                    $old_voted_img_id = $row->vote_img;
                }
                $extracted_img_id = explode(',',$old_voted_img_id);
                return $extracted_img_id;
        }
        
}